#include "buttongroup.h"
#include "ui_buttongroup.h"

#include <QMouseEvent>
#include <QSignalMapper>
#include <QDebug>

buttongroup::buttongroup(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::buttongroup)
{
    ui->setupUi(this);

    m_mapper = new QSignalMapper(this);
    m_curbtn = ui->myfile;
    m_curbtn->setStyleSheet("color:red");

    m_btns.insert(ui->myfile->text(), ui->myfile);
    m_btns.insert(ui->sharelist->text(), ui->sharelist);
    m_btns.insert(ui->download->text(), ui->download);
    m_btns.insert(ui->transform->text(), ui->transform);
    m_btns.insert(ui->switchUser->text(), ui->switchUser);

    QMap<QString, QToolButton*>::iterator it;
    for(it = m_btns.begin(); it != m_btns.end(); ++it)
    {
        m_mapper->setMapping(it.value(), it.value()->text());
        connect(it.value(), SIGNAL(clicked(bool)), m_mapper, SLOT(map()));
    }
    connect(m_mapper, SIGNAL(mapped(QString)), this, SLOT(slotButtonClick(QString)));
}

buttongroup::~buttongroup()
{
    delete ui;
}

void buttongroup::mouseMoveEvent(QMouseEvent *event)
{
    if(event->buttons() & Qt::LeftButton)
    {
        QPoint pos = event->globalPos() - m_pos;
        m_parent->move(pos);
    }
}

void buttongroup::mousePressEvent(QMouseEvent *event)
{
    if(event->button() == Qt::LeftButton)
    {
        m_pos = event->globalPos() - m_parent->geometry().topLeft();
    }
}

void buttongroup::setParent(QWidget* parent)
{
    m_parent = parent;
}

void buttongroup::on_mw_close_clicked()
{
    m_parent->close();
}

void buttongroup::on_mw_min_clicked()
{
    m_parent->showMinimized();
}

void buttongroup::slotButtonClick(QString text)
{
    QToolButton* btn = m_btns[text];
    if(btn == m_curbtn && btn != ui->switchUser)
    {
        return;
    }
    m_curbtn->setStyleSheet("color:black");
    btn->setStyleSheet("color:blue");

    m_curbtn = btn;
    if(text == ui->myfile->text())
    {
        emit sigMyFile();
    }
    else if(text == ui->sharelist->text())
    {
        emit sigShareList();
    }
    else if(text == ui->download->text())
    {
        emit sigDownload();
    }
    else if(text == ui->transform->text())
    {
        emit sigTransform();
    }
    else if(text == ui->switchUser->text())
    {
        emit sigSwitchUser();
    }
}
